﻿@inject AuthenticationStateProvider AuthProvider
@inject IDataService DataService
@inject IDataService DataService
@inject IThemeProvider ThemeProvider

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=550, user-scalable=no" />
    <title>Remotely</title>
    <base href="/" />
    <link href="css/site.css" rel="stylesheet" />
    <link href="Remotely_Server.styles.css" rel="stylesheet" />
    <script src="interop.js"></script>

    @if (_theme == Theme.Dark)
    {
        <link rel="stylesheet" href="/css/Themes/darkly.min.css" />
        <link rel="stylesheet" href="/css/Themes/darkly.custom.css" />
    }
    else
    {
        <link rel="stylesheet" href="/css/Themes/yeti.min.css" />
        <link rel="stylesheet" href="/css/Themes/yeti.custom.css" />
    }

    <HeadOutlet @rendermode="RenderModeForPage" />
</head>
<body>
    <Routes @rendermode="RenderModeForPage" />
    <script src="_framework/blazor.web.js"></script>
    <script src="/lib/bootstrap/bootstrap.bundle.min.js"></script>
</body>
</html>

@code
{
    [CascadingParameter]
    public required HttpContext HttpContext { get; set; }

    private RemotelyUser? _user;
    private Theme _theme;

    protected override async Task OnInitializedAsync()
    {
        _theme = await ThemeProvider.GetEffectiveTheme();
        var state = await AuthProvider.GetAuthenticationStateAsync();
        if (state.User.Identity is { IsAuthenticated: true } identity)
        {
            var userResult = await DataService.GetUserByName($"{identity.Name}");
            if (userResult.IsSuccess)
            {
                _user = userResult.Value;
            }
        }
        await base.OnInitializedAsync();
    }


    private IComponentRenderMode? RenderModeForPage => 
        HttpContext.Request.Path.StartsWithSegments("/Account")
            ? null
            : InteractiveServer;
}